package com.xpoint.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.Category;
import com.xpoint.backend.entity.Dish;
import com.xpoint.backend.entity.Setmeal;
import com.xpoint.backend.mapper.CategoryMapper;
import com.xpoint.backend.service.CategoryService;
import com.xpoint.backend.service.DishService;
import com.xpoint.backend.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    @Override
//    分页查询
    public R<Page> fnidPageByCategory(Long page, Long pageSize) {
//        创建一个分页对象
        Page<Category> page1 = new Page(page, pageSize);
//        创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        条件：根据Sort进行升序排序
        queryWrapper.orderByAsc(Category::getSort);
//        分页查询
        Page<Category> page2 = page(page1, queryWrapper);
        if (page2!=null){
            return R.success(page2);
        }
        return R.error("查询失败");
    }

    @Override
//    根据id删除分类
    public R<String> removeCategoryById(Long id) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Dish::getCategoryId,id);
        dishService.remove(lambdaQueryWrapper1);

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(Setmeal::getCategoryId,id);
        setmealService.remove(lambdaQueryWrapper2);
        if (removeById(id)){
            return R.success("删除成功");
        }
        return R.success("删除失败");
    }

    @Override
//    添加分类
    public R<String> addCategory(Category category) {
        if (category!=null){
            if (save(category)){
                return R.success("成功");
            }
            return R.success("添加异常");
        }
        return R.success("添加失败，为空");
    }

    @Override
//    更新分类
    public R<String> updateCategory(Category category) {
        if (category!=null){
            if (updateById(category)) {
                return R.success("成功");
            }
            return R.success("失败");
        }
        return R.success("为空");
    }

    @Override
//    根据type查询分类，1是菜品分类，2是套餐分类
    public R<List<Category>> findDishTypeList(Long type) {
//        创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        根据Sort字段升序，更新时间降序
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
//        如果type不为空，那么就添加sql查询，查询type=传过来的typeValue
        queryWrapper.eq(type!=null,Category::getType,type);
//        执行查询
        List<Category> list = list(queryWrapper);
        return R.success(list);
    }
}
